Skip to content

Codegen audit: tighten P1/P3/P4/P5 across SDK boundaries#4

Merged
caballeto merged 4 commits into
mainfrom
chore/codegen-audit-phase-0-and-1
Apr 21, 2026
Merged

Codegen audit: tighten P1/P3/P4/P5 across SDK boundaries#4
caballeto merged 4 commits into
mainfrom
chore/codegen-audit-phase-0-and-1

Conversation

@caballeto
Copy link
Copy Markdown
Member

Part of the cross-repo codegen audit (devhelmhq/mono#246). Aligns the Python SDK with policies P1 / P2 / P3 / P4 / P5 from cowork/design/040-codegen-policies.md.

Summary

Codegen pipeline (existing)

  • feat(codegen) + chore(p5) from earlier in the branch enabled strict policies on the generated Pydantic models, eliminated remaining cast() / # type: ignore in HTTP + pagination, and added scripts/regen-from.sh for the spec-evolution hook.

This commit (77555a4) — final tightening

  • P1 — response envelope strictness. New parse_strict_envelope helper in _validation.py enforces {data: T} envelopes with extras forbidden. DeployLock.current() migrated to it (replaces a one-off _CurrentLockEnvelope BaseModel).
  • P3 — RootModel inner variant strictness. Tests now assert that every RootModel inner variant enforces extra='forbid', closing the gap where discriminated unions could silently accept unknown fields.
  • P4 — narrowed exception scope. _pagination.py switched from except Exception to except ValidationError so transport / programmer errors surface intact instead of being miscategorized as validation failures.
  • P5 — disallow_any_explicit globally. Enabled in pyproject.toml (mypy strict mode). JSON-boundary modules carry minimal targeted overrides; the resource modules are now Any-free, asserted by tests/test_typing.py.

Tests

  • New tests/test_validation_helpers.py for parse_strict_envelope (data-present / data-null / extras / nested errors).
  • tests/test_schemas.py gained TestRootModelStrictness class.
  • tests/test_typing.py refactored to assert resource modules are explicit-Any-free.

Spec-evolution coverage (in monorepo PR)

tests/surfaces/evolution/surfaces/test_sdk_python.py in mono#246 already pins P1 / P3 / P4 against this branch (response extras, oneOf variant drops, taxonomy class hierarchy + transport-error wrapping, etc.).

Test plan

  • make typecheck — mypy strict + disallow_any_explicit clean
  • make test — full unit suite passing
  • make lint + make format — clean
  • Surface harness in tests/surfaces/sdk-python/ passes against this branch
  • tests/surfaces/evolution/surfaces/test_sdk_python.py — full pass + documented xfails preserved

Made with Cursor

…ness hook

Phase 0/1 of the cross-repo codegen audit. Aligns with the policy doc
landing in monorepo at `cowork/design/040-codegen-policies.md`:

- P1 (response extras=forbid) + P2 (request extras=forbid): inject
  `model_config = ConfigDict(extra='forbid')` into every generated
  model via a new `scripts/inject_strict_config.py` post-processor
  wired through `scripts/typegen.sh`. Skips RootModels (Pydantic
  rejects `extra` there) and tolerates StrEnum member-name shadowing.
- P4 (three-class error taxonomy): split `DevhelmError` into
  `DevhelmValidationError` (local schema mismatches),
  `DevhelmApiError` (HTTP 4xx/5xx), and `DevhelmTransportError`
  (network/TLS/timeout) — same names that sdk-js, cli, mcp-server
  and the terraform-provider expose. `_http.py` now wraps the
  underlying `httpx` transport errors. No backwards-compat aliases
  (no customers).
- P5 (zero casts outside generated): drop the mypy `_generated.py`
  exclusion and fix the resulting type errors by tightening the
  injector and the StrEnum-collision allowlist.

Adds `scripts/regen-from.sh` so the upcoming spec-evolution harness
in monorepo can regenerate this SDK from a mutated spec without
needing to know the internal layout.

Tests refreshed to assert against the new error hierarchy.

Made-with: Cursor
Phase 2 cleanup: remove residual `cast()` calls in `_pagination.py` and
`# type: ignore` comments in `_http.py` so the SDK has zero type-system
escapes outside generated files (P5).

Made-with: Cursor
* P1 (response strict): add `parse_strict_envelope` helper to
  `_validation.py` so endpoints with nullable `data` (today only
  GET /api/v1/deploy/lock) reject unknown top-level envelope keys
  without forcing the resource layer to declare a `BaseModel`
  subclass (which would trip the new `disallow_any_explicit`).
* P3 (validation surfaces): narrow broad `except Exception` in
  `_pagination.py` to `ValidationError`, and pass the inner errors
  + cause into `DevhelmValidationError` so callers can route on
  field-level failures and stack traces survive.
* P5 (no-Any in user-facing layer): enable
  `disallow_any_explicit = true` globally with explicit overrides
  for the JSON-boundary modules (`_errors`, `_http`, `_pagination`,
  `_validation`, `_generated`). Renamed the typing test to
  `test_resource_layer_is_any_free` and rewrote it against the new
  contract.
* Lock in `RootModel` strictness with a focused suite covering both
  the scalar wrapper (`SubscribedEvent`) and the discriminated
  union (`CheckTypeDetailsDto`); these were silently skipped by
  `inject_strict_config` and thus never directly verified.
* Lock in `parse_strict_envelope` semantics (extras forbidden,
  optional null, structured Pydantic error propagation) in a new
  `tests/test_validation_helpers.py`.

Made-with: Cursor
CI's `ruff format --check` was failing on 7 files that hadn't been
reformatted since the last formatter rule update. Applied the formatter.

Also fixes a latent brittleness: `test_typing.py` allow-listed the one
documented `# type: ignore` by full `path:lineno: line` triple, so any
unrelated reflow above the suppression site (like this very format pass
shifting line 126 → 122 in `_validation.py`) silently turns the test red
on changes that have nothing to do with type ignores.

The allow-list is now keyed on `(relative_path, stripped_line)` —
adding/removing/moving suppressions still reds the test, but unrelated
formatting churn doesn't. The error message keeps the full
`file:line:` offender list for fast diagnosis.

Verified: ruff format --check, ruff check, mypy, and `pytest` (700
passed) all green locally.

Made-with: Cursor
@caballeto caballeto merged commit da35cf3 into main Apr 21, 2026
4 checks passed
@caballeto caballeto deleted the chore/codegen-audit-phase-0-and-1 branch April 21, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant